package main

import (
	"ai-curd/controller"
	"ai-curd/repository"
	"ai-curd/repository/ent"
	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
	"log"
	"net/http"
	"time"
)

func main() {

	db, err := ent.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/bbs?charset=utf8mb4&parseTime=true&loc=Local")
	if err != nil {
		log.Fatalf("ent.Open: %s", err)
	}

	srv := gin.Default()

	srv.GET("/ping", func(c *gin.Context) {
		time.Sleep(3 * time.Second)
		c.JSON(http.StatusOK, gin.H{
			"message": "pong",
		})
	})

	handler := new(controller.Handler)

	// Article routes
	repoArticle := repository.NewArticle(db)
	ctrArticle := controller.NewArticle(repoArticle)
	srv.GET("/article", handler.Wrapper(ctrArticle.One))
	srv.GET("/articles", handler.Wrapper(ctrArticle.Many))
	srv.POST("/article/create", handler.Wrapper(ctrArticle.Create))
	srv.POST("/article/modify", handler.Wrapper(ctrArticle.Modify))

	// User routes
	repoUser := repository.NewUser(db)
	ctrUser := controller.NewUser(repoUser)
	srv.GET("/user", handler.Wrapper(ctrUser.One))
	srv.GET("/users", handler.Wrapper(ctrUser.Many))
	srv.POST("/user/create", handler.Wrapper(ctrUser.Create))
	srv.POST("/user/modify", handler.Wrapper(ctrUser.Modify))
	srv.POST("/user/delete", handler.Wrapper(ctrUser.Delete))

	srv.Run(":80")
}
